# -*- coding: utf-8 -*-

#  Copyright (C) 2008 - Eugene Khorev
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330,
#  Boston, MA 02111-1307, USA.

ui_str = '''<ui>
  <menubar name="MenuBar">
    <menu name="EditMenu" action="Edit">
      <placeholder name="EditOps_6">
        <menuitem name="ToggleBookmark" action="ToggleBookmark"/>
      </placeholder>
    </menu>
  </menubar>
</ui>
'''

import pygtk
pygtk.require("2.0")
import gtk
import gedit
import time
import os
import sys
import bookmarks
import config_dlg

class window_helper:
    def __init__(self, plugin, window):
        self._window = window
        self._plugin = plugin

        # Insert menu items
        self._insert_menu()
    
    def deactivate(self):
        # Remove any installed menu items
        self._remove_menu()

        self._window = None
        self._plugin = None
        self._action_group = None

    def update_ui(self):
        self._action_group.set_sensitive(self._window.get_active_document() != None)
        
        # Swicth bookmark store for current document
        doc = self._window.get_active_document()
        if doc:
            uri = doc.get_uri()
            self._plugin._tree.set_model(self._plugin._bookmarks.get_store(uri))

    def _insert_menu(self):
        # Get the GtkUIManager
        manager = self._window.get_ui_manager()

        # Create a new action
        self._act_toggle = gtk.Action("ToggleBookmark", _("Toggle bookmark"), _("Toggle bookmark"), self._plugin._icon)
        self._act_toggle.connect("activate", self._on_toggle_bookmark)
        
        self._action_group = gtk.ActionGroup("AdvancedBookmarksActions")
        self._action_group.add_action_with_accel(self._act_toggle, "<Ctrl>b")

        # Insert the action group
        manager.insert_action_group(self._action_group, -1)

        # Merge the UI
        self._ui_id = manager.add_ui_from_string(ui_str)
        
    def _remove_menu(self):
        # Get the GtkUIManager
        manager = self._window.get_ui_manager()

        # Remove the ui
        manager.remove_ui(self._ui_id)

        # Remove the action group
        manager.remove_action_group(self._action_group)

        # Make sure the manager updates
        manager.ensure_update()
        
    def _on_toggle_bookmark(self, action):
        # Get document uri
        doc = self._window.get_active_document()
        uri = doc.get_uri()
        
        if uri:
            # Get current position
            text_iter = doc.get_iter_at_mark(doc.get_insert())

            # Get current line number (strarting from 0)
            line = text_iter.get_line()

            # Get position of the current and the next lines
            start = doc.get_iter_at_line(line)
            end   = doc.get_iter_at_line(line+1)
            
            # Check if we are at the last line
            if start.get_offset() == end.get_offset():
                end = doc.get_end_iter()
            
            # Get line text
            comment = doc.get_text(start, end, False).strip()
            
            self._plugin._bookmarks.toggle(uri, line+1, comment)
            self._plugin._tree.set_model(self._plugin._bookmarks.get_store(uri))

# ex:ts=4:et:
